package com.ivy.wechatforclient.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ivy.wechatforclient.R;
import com.ivy.wechatforclient.entity.Message;

import java.util.List;

/**
 * Created by Lenovo on 2017/12/7.
 */

public class MessageAdapter extends RecyclerView.Adapter<MessageAdapter.ViewHolder> {
    private List<Message> msgList ;
    private String sender;
    private String receiver;
    static class ViewHolder extends RecyclerView.ViewHolder{
        LinearLayout ll_left;
        LinearLayout ll_right;
        TextView tv_left;
        TextView tv_right;

        public ViewHolder(View view) {
            super(view);
            ll_left = (LinearLayout)view.findViewById(R.id.ll_left);
            ll_right = (LinearLayout) view.findViewById(R.id.ll_right);
            tv_left = (TextView) view.findViewById(R.id.tv_left);
            tv_right = (TextView) view.findViewById(R.id.tv_right);
        }
    }
    public MessageAdapter(List<Message> msgList ,String sender, String receiver) {
        this.msgList = msgList;
        this.sender = sender;
        this.receiver = receiver;
    }
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_msg , parent , false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Message message = msgList.get(position);
        if(message.getReceiver().equals(sender)) {     //左边接收方
            holder.ll_left.setVisibility(View.VISIBLE);
            holder.ll_right.setVisibility(View.GONE);
            holder.tv_left.setText(message.getContent());
        } else if(message.getReceiver().equals(receiver)) {     //右边发送方
            holder.ll_left.setVisibility(View.GONE);
            holder.ll_right.setVisibility(View.VISIBLE);
            holder.tv_right.setText(message.getContent());
        }
    }

    @Override
    public int getItemCount() {
        return msgList.size();
    }



}
